package com.qumitech.visualnovel.service.impl;

import com.qumitech.visualnovel.datasource.console.ConsoleChannelConfigMapper;
import com.qumitech.visualnovel.model.dao.console.ConsoleChannelConfig;
import com.qumitech.visualnovel.service.ConsoleChannelConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * console_backend
 *
 * @author zhangbc
 * @version 1.0.0
 * @Date 2020/7/8 11:32
 **/
@Service
public class ConsoleChannelConfigServiceImpl implements ConsoleChannelConfigService {
    @Autowired
    ConsoleChannelConfigMapper channelConfigMapper;

    @Override
    public Map<String, List<String>> getConsoleChannelOptions() {
        List<ConsoleChannelConfig> list = channelConfigMapper.getChannelOptions(null);
        Map<String, List<String>> channels = new HashMap<>();
        for (ConsoleChannelConfig item: list) {
            List<String> values = new ArrayList<>();
            if (channels.containsKey(item.getName())) {
                if (item.getSecondName() != null && !"".equals(item.getSecondName())) {
                    values = channels.get(item.getName());
                    values.add(item.getSecondName());
                    channels.put(item.getName(), values);
                }
            } else {
                if (item.getSecondName() == null) {
                    channels.put(item.getName(), values);
                } else {
                    values.add(item.getSecondName());
                    channels.put(item.getName(), values);
                }
            }
        }

        return channels;
    }

    @Override
    public List<String> listSubChannel(String secondName) {
        return channelConfigMapper.getChannelOptions(secondName).stream()
                .filter(e -> StringUtils.isNotEmpty(e.getSecondName()))
                .map(ConsoleChannelConfig::getSecondName).collect(Collectors.toList());
    }
}
